package com.mzy.NetworkProgram.networkinterfacebasic.dns;

import org.testng.annotations.Test;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author mzy
 * @Date 2022/7/6 10:46
 * @Version 1.0
 */
public class DnsGet {

    private static final String baidu ="www.baidu.com";
    private static final String guGe ="www.google.com";
    private static final String csdn ="blog.csdn.net";
//    private static final String ip ="14.215.177.385";
    private static final String ip ="14.215.177.383";

    @Test
    public void dnsDemo(){
        //        //获取本机IP地址
//        System.out.println(InetAddress.getLocalHost().getHostAddress());
//        //获取www.baidu.com的地址
//        System.out.println(InetAddress.getByName("www.baidu.com"));
//        //获取www.baidu.com的真实IP地址
//        System.out.println(InetAddress.getByName("www.baidu.com").getHostAddress());
//        //获取配置在HOST中的域名IP地址
//        System.out.println(InetAddress.getByName("eureka7001.com").getHostAddress());
//

        try {
            InetAddress[] addresses = InetAddress.getAllByName(baidu);
            for (int i = 0; i < addresses.length; i++) {
                System.out.println(baidu + "[" + i + "]: "+ addresses[i].getHostAddress());
                if (baidu.equals(addresses[i].getHostAddress())){
                    System.out.println(baidu + "是IP");
                }else {
                    System.out.println(baidu + "是域名");
                }
            }
        } catch (UnknownHostException e) {
            System.out.println("ip或域名解析错误");
            System.out.println("具体异常 ： "+ e);
        }
    }
}
